<?php
class ArcControl extends Control{
	function __auto(){
		C('debug',0);
	}
	//显示文章首页视图
	function index(){
		$db = K("arc");
        $total = $db->where('recycle=1')->count(); //获得总条数
        $page = new page($total,12); //设置分页每8条记录为一页
        $data = $db->order('aid')->where('recycle=1')->limit($page->limit())->all();
        $this->assign("data", $data);
        $this->assign('page',$page->show(2));
        $this->display();

	}
    // 回收站
    function recycle(){
        $db = K("arc");
        $total = $db->where('recycle=2')->count(); //获得总条数
        $page = new page($total,12); //设置分页每8条记录为一页
        $data = $db->order('aid')->where('recycle=2')->limit($page->limit())->all();
        $this->assign("data", $data);
        $this->assign('page',$page->show(2));
        $this->display();
    }
	//显示添加文章视图
	function addshow(){
        //获得所有文章属性
        $db = M("flag");
        $flag = $db->all();
        $this->assign("flag", $flag);
		//如果没有选择添加栏目，获得所有栏目
        if (!isset($_GET['cid'])) {
            $this->assignCategoryData();
        }
		$this->display();
	}
	//执行添加文章
	function addarc(){
		$data = $_POST;
        $data['addtime'] = $_SERVER['REQUEST_TIME'];
        $flag = $data['flag'];
        $data['thumbimg'] = isset($data['thumbimg'][1]) ? $data['thumbimg'][1]['path'] : ''; //缩略图
        $db = M("arc");
        if ($insertId = $db->add($data)) {
            foreach ($flag as $f) {
                $arc_flag = array(
                    "flag_fid" => $f,
                    "arc_aid" => $insertId
                );
                $db->table("flag_arc")->add($arc_flag);
            }
            // $htmlObj = new HtmlControl();
            // $htmlObj->arc($insertId);
            $this->success("发表文章成功", "index");
        } else {
            $this->error("文章添加失败");
        }
	}
    //显示编辑文章视图
    function editarc(){
        if ($aid = $this->_get("aid")) {
            $this->assignCategoryData(); //分配栏目数据
            $db = M('arc');
            $field = $db->find($aid); //获得编辑文章的原数据
            $thumbimg = array(
                array(
                    "alt" => "图片描述",
                    "path" => $field['thumbimg'],
                    "thumb" => array($field['thumbimg'])
                )
            );
            $this->assign("thumbimg", $thumbimg);
            $this->assign("field", $field);
            $this->display();
        }
    }
    // 执行编辑保存内容
    function save(){
        $data = $_POST;
        $data['thumbimg'] = isset($data['thumbimg'][1]['path']) ? $data['thumbimg'][1]['path'] : ''; //缩略图
        $db = M("arc");
        if ($db->save($data) >= 0) {
            $this->success("文章修改成功", "index");
        } else {
            $this->error("编辑文章失败，请重试");
        }

    }
    // 文章预览
    function preview(){
        if ($aid = $this->_get("aid")) {
            $db = M('arc');
            $field = $db->field('title,aid,addtime')->find($aid);
            //获得文章的数据
            // $thumbimg = array(
            //     array(
            //         "alt" => "图片描述",
            //         "path" => $field['thumbimg'],
            //         "thumb" => array($field['thumbimg'])
            //     )
            // );
            // $field['src']=$thumbing['path'];
            // $this->assign("thumbimg", $thumbimg);
            $this->assign("field", $field);
            $this->display();
        }//if
    }
    // 文章预览异步获得文章内容
    function showarc(){
        $aid=$_POST['aid'];
        $db = M('arc');
        if($field1 = $db->find($aid)){
            echo json_encode(array('status' => 1,'content'=>$field1,'jpg'=>array(
              "alt" => "图片描述",
              "path" => $field1['thumbimg'],
              "thumb" => array($field1['thumbimg'])
              )
        ));
        } else {
            echo json_encode(array('status' => 0));
        }
    }
    //删除文章(彻底删除)
    function delarc(){
        $aid=$_GET['aid'];
        $db=M('arc');
        if($db->del($aid)){
            $this->success('删除成功');
        }else{
            $this->error('删除失败,请重试');
        }
    }
    // 删除文章,放入回收站(单个)
    function delarc_one(){
        $aid=$_POST['aid'];
        $db=M('arc');
        $data=array(
            'aid'=>$aid,
            'recycle'=>2
            );
        // $sql='update ec_arc set recycle=2 where aid=$aid';
        if($db->update($data)){
            echo json_encode(array('status' => 1));
        }else{
           echo json_encode(array('status' => 0));
        } 
    }
    // 删除文章,放入回收站(多个)
    function delarc_all(){
        $arr=$_POST['aid'];//获得js传过来的文章id(数组);
        foreach($arr as $v){ 
            $db=M('arc');  
            $data=array(
                'aid'=>$v,
                'recycle'=>2
            );
            $db->update($data);
        }
        echo json_encode(array('status' => 1));
        
    }
    //还原文章(单个)
    function restore_one(){
        $aid=$_POST['aid'];
        $db=M('arc');
        $data=array(
            'aid'=>$aid,
            'recycle'=>1
            );
        // $sql='update ec_arc set recycle=1 where aid=$aid';
        if($db->update($data)){
            echo json_encode(array('status' => 1));
        }else{
           echo json_encode(array('status' => 0));
        } 
    }
    // //还原文件(多个)
    function restore_more(){
        $aid=$_POST['aid'];
        foreach($aid as $v){
            $db=M('arc');  
            $data=array(
                'aid'=>$v,
                'recycle'=>1
            );
            $db->update($data);
        }
        echo json_encode(array('status' => 1));
    }
    
    //向模板分配栏目
    private function assignCategoryData() {
        $db = M("category");
        $result = $db->all();
        $data = Data::channel($result, "cid", "pid", 0, "", 2, "--");
        $this->assign("category", $data);
    }

    // 审核文章
    public function checkarc(){
        $db = K("arc");
        $total = $db->where('recycle=1')->where('shenhe=0')->count(); //获得总条数
        $page = new page($total,12); //设置分页每8条记录为一页
        $data = $db->order('aid')->where('recycle=1')->where('shenhe=0')->limit($page->limit())->all();
        $this->assign("data", $data);
        $this->assign('page',$page->show(2));
        $this->display();
    }
    // 审核通过
    public function through(){
        $db=M('arc');
        $data=array('aid'=>$_GET['aid'],'shenhe'=>1);
        if($db->save($data)){
            $this->success('已审核','checkarc');
        }else{
            $this->error('审核失败');
        }
    }
    //查看评论
    public function showdiscuss(){
        echo 1111111111111;

    }
    //审核评论
    public function checkdiscuss(){
        echo 22222222222222222;
    }
}
?>
